package hotel.action;

import hotel.form.CheckRequestForm;
import hotel.service.RequestService;
import hotel.service.ServiceManager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
/**
 * This class get request from DB and check 'status'
 * @author Alex
 *
 */
public class CheckRequestAction extends Action 
{
	private static final Logger logger = Logger.getLogger(CheckRequestAction.class);
	private static final String CHECKREQUESTACTION_ERROR = "Error in CheckRequestAction:";
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest httpRequest, HttpServletResponse response)
		    throws Exception 
		    {
				String mapper = ForwardActionTo.ERROR;
				CheckRequestForm checkRequestForm = (CheckRequestForm) form;
				if (checkRequestForm != null)
				{
					try
					{
						ActionMessages messages = new ActionMessages();
						RequestService requestService = ServiceManager.getDefault().getRequestService();
						if (requestService.checkStatus(checkRequestForm.getId()) != null)
							messages.add("request_status", new ActionMessage("request.status." + requestService.checkStatus(checkRequestForm.getId())));
						else 
							messages.add("request_status", new ActionMessage("request.status.accepted.not"));
			   	 	 	saveMessages(httpRequest,messages);
			   	 	 	
			   	 	 	mapper = ForwardActionTo.SUCCESS;
					}
					catch(NullPointerException e)
					{
						logger.error(CHECKREQUESTACTION_ERROR + e);
				 	}
		    	}
				return mapping.findForward(mapper);
		    }

}
